<?php 
class ControllerProductManufacturer extends Controller {  
	public function index() {
        $this->language->load('product/manufacturer');

        $this->load->model('catalog/manufacturer');
        $this->load->model('tool/seo_url'); 
        $this->load->model('tool/image');
        
        $this->document->breadcrumbs = array();         
        $this->document->breadcrumbs[] = array(
            'href'      => HTTP_SERVER . 'index.php?route=common/home',
            'text'      => $this->language->get('text_home'),
            'separator' => FALSE
        );
        
        $this->data['template'] = $this->config->get('config_template');        
		$this->data['description'] = '';
		
        $manufacturers_list = $this->model_catalog_manufacturer->getManufacturers();                           
        if ($manufacturers_list) {                        
			$meta_title = $this->config->get('config_title'); //$this->config->get('config_meta_title');       
            $this->document->title = $meta_title; 
            $this->document->keywords = $this->config->get('config_meta_keywords');                      
            $this->document->description = $this->config->get('config_meta_description');    
            
            $this->data['heading_title'] = $this->language->get('heading_title');                
            
            $manufacturers = array();
            foreach($manufacturers_list as $item){                
                $logo = $item['logo'] ? $item['logo'] : 'no_image.jpg'; 
                $link = HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $item['manufacturer_id'];    
                
                $manufacturers[] = array(
                    'manufacturer_id'   => $item['manufacturer_id'],
                    'name'              => $item['name'],
                    'sort_order'        => $item['sort_order'],
                    'image'             => $this->model_tool_image->resize($logo, 214, 50),
                    //'logo'              => $logo,
                    'href'              => $this->model_tool_seo_url->rewrite($link),
                );                
            }
            $this->data['manufacturers'] = $manufacturers; 
            
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/manufacturer_list.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/product/manufacturer_list.tpl';
            } else {
                $this->template = 'default/template/product/manufacturer_list.tpl';
            }    

            $this->children = array(
                'common/header',
                'common/footer',
                'common/column_left',
                'module/category',
                'module/services',
                'module/newsletter',
            );        

            $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
        }
        else {
            $this->document->title = $this->language->get('heading_title');
            $this->data['heading_title'] = $this->language->get('heading_title');
            $this->data['text_error'] = $this->language->get('text_error_desc');            
            $this->data['button_continue'] = $this->language->get('button_continue');
            $this->data['continue'] = false;// HTTP_SERVER . 'index.php?route=common/home';
                  
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
            } else {
                $this->template = 'default/template/error/not_found.tpl';
            }
            
            $this->children = array(                
                'common/header',
                'common/footer',    
                'common/column_left',
            );        
            
            $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
        } 
    }
	
    public function category() {
        $this->language->load('product/manufacturer');

        $this->load->model('catalog/manufacturer');
        $this->load->model('tool/seo_url'); 
        $this->load->model('tool/image');
        
        $this->document->breadcrumbs = array();         
        $this->document->breadcrumbs[] = array(
            'href'      => HTTP_SERVER . 'index.php?route=common/home',
            'text'      => $this->language->get('text_home'),
            'separator' => FALSE
        );

        $this->data['breadcrumbs'] = array();
        $this->data['breadcrumbs'][] = array(
            'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer'),
            'text'      => $this->language->get('heading_title'),
            'separator' => FALSE
        );

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['template'] = $this->config->get('config_template');
        $category = isset($this->request->get['path']) ? $this->request->get['path'] : '';
        $current_category = explode('_', $category);
        $current_category = array_pop($current_category);

        //$manufacturers_list = $this->model_catalog_manufacturer->getManufacturers();                   
        $manufacturers_list = $this->model_catalog_manufacturer->getManufacturersByCategory($current_category);
        if ($manufacturers_list) {                        
            if($current_category > 0){
                $meta_title = $this->model_catalog_manufacturer->getManufacturerMetaTitle(0, $current_category);
				
				$this->load->model('catalog/category');
				$category_info = $this->model_catalog_category->getCategory($current_category);
				$this->data['description'] = html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8');
                $this->data['heading_title'] = $category_info['name'];

                $this->data['breadcrumbs'][] = array(
                    'href'      => FALSE,
                    'text'      => $category_info['name'],
                    'separator' => FALSE
                );
            }   
            else{
                $meta_title = $this->config->get('config_meta_title');
				$this->data['description'] = '';
            }   
			
            $this->document->title = $meta_title; 
            $this->document->keywords = $this->config->get('config_meta_keywords');                      
            $this->document->description = $this->config->get('config_meta_description');
            
            $manufacturers = array();
            foreach($manufacturers_list as $item){                
                $logo = $item['logo'] ? $item['logo'] : 'no_image.jpg'; 
                $link = HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $item['manufacturer_id'] . '&path=' . $category; //'&category_id=' . $category;    
                
                $manufacturers[] = array(
                    'manufacturer_id'   => $item['manufacturer_id'],
                    'name'              => $item['name'],
                    'sort_order'        => $item['sort_order'],
                    'image'             => $this->model_tool_image->resize($logo, 214, 50),
                    //'logo'              => $logo,
                    'href'              => $this->model_tool_seo_url->rewrite($link),
                );                
            }
            $this->data['manufacturers'] = $manufacturers; 
            
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/manufacturer_category.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/product/manufacturer_category.tpl';
            } else {
                $this->template = 'default/template/product/manufacturer_category.tpl';
            }    

            $this->children = array(
                'common/header',
                'common/footer',
                'common/column_left',
                'module/category',
                'module/services',
                'module/newsletter',
            );        

            $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
        }
        else {
            $this->document->title = $this->language->get('heading_title');
            $this->data['heading_title'] = $this->language->get('heading_title');
            $this->data['text_error'] = $this->language->get('text_error_desc');            
            $this->data['button_continue'] = $this->language->get('button_continue');
            $this->data['continue'] = false;// HTTP_SERVER . 'index.php?route=common/home';
                  
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
            } else {
                $this->template = 'default/template/error/not_found.tpl';
            }
            
            $this->children = array(                
                'common/header',
                'common/footer',    
                'common/column_left',
            );        
            
            $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
        } 
    }
    
    public function viewManufacturer() { 
        $this->language->load('product/manufacturer');

        $this->load->model('catalog/manufacturer');
        $this->load->model('catalog/product');
        $this->load->model('catalog/category');  
        $this->load->model('tool/seo_url'); 
        $this->load->model('tool/image');
        $this->load->model('catalog/discount');

        // Compatible only, does not use in this function
        $this->document->breadcrumbs = array();        
        $this->document->breadcrumbs[] = array(
            'href'      => HTTP_SERVER . 'index.php?route=common/home',
            'text'      => $this->language->get('text_home'),
            'separator' => FALSE
        );

        if (isset($this->request->get['manufacturer_id'])) {
            $manufacturer_id = $this->request->get['manufacturer_id'];
        } else {
            $manufacturer_id = 0;
        }
                
        //$category_path = $this->request->get['category_id'];
        if (isset($this->request->get['path'])){
            $category_path = $this->request->get['path'];
            $category = explode('_', $category_path);
            $category = array_pop($category);
        }
        else{
            $category = 0;    
        }
         
        $this->data['template'] = $this->config->get('config_template');
        $this->data['breadcrumbs'] = array();
        if($category > 0){
            $this->data['back'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer&path=' . $category_path);

            $category_info = $this->model_catalog_category->getCategory($category);
            $this->data['breadcrumbs'][] = array(
                'href'      => $this->data['back'],
                'text'      => $category_info['name'],
                'separator' => ' >> ',
            );
        }
        else{
            $this->data['back'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home');

            $this->data['breadcrumbs'][] = array(
                'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer'),
                'text'      => 'Brands',
                'separator' => ' >> ',
            );
        }

        $manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($manufacturer_id);      
        if ($manufacturer_info) {
            if($category > 0){
                $href = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id']) . '&path=' . $category_path; //'&category_id=' . $category_path;
            }
            else{
                $href = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id']);
            }

            // Compatible only, does not use in this function
            $this->document->breadcrumbs[] = array(
                'href'      => $href,
                'text'      => $manufacturer_info['name'],
                'separator' => $this->language->get('text_separator')
            );

            $this->data['breadcrumbs'][] = array(
                'href'      => '',
                'text'      => $manufacturer_info['name'],
                'separator' => '',
            );
             
            $meta_title = $manufacturer_info['meta_title'] ? $manufacturer_info['meta_title'] : $manufacturer_info['name'];
            if($category > 0){
                $page_title = $this->model_catalog_manufacturer->getManufacturerMetaTitle($manufacturer_id, $category);
                if($page_title){
                    $meta_title = $page_title;   
                }
            }                 
            $this->document->title = $meta_title;                         
            $this->document->keywords = $manufacturer_info['meta_keywords'];             
            $this->document->description = $manufacturer_info['meta_description'];                                   
            
            $this->data['heading_title'] = $manufacturer_info['name']; 
            $this->data['text_sort'] = $this->language->get('text_sort');
            
            $this->data['title'] = $manufacturer_info['title'];
            if($manufacturer_info['description']){
                $this->data['description'] = html_entity_decode($manufacturer_info['description'], ENT_QUOTES, 'UTF-8'); 
            }
            else{
                $this->data['description'] = 'Der er ingen beskrivelse af dette Mærker';   
            }
            
            $this->data['images'] = array();
            $image = $manufacturer_info['image'] ? $manufacturer_info['image'] : 'no_image.jpg';
            $image1 = $manufacturer_info['image1'] ? $manufacturer_info['image1'] : '';
            $image2 = $manufacturer_info['image2'] ? $manufacturer_info['image2'] : '';
            $image3 = $manufacturer_info['image3'] ? $manufacturer_info['image3'] : '';
            
            $this->data['images'][] = $this->model_tool_image->resize($image, 207, 243);
            if($image1){
                $this->data['images'][] = $this->model_tool_image->resize($image1, 207, 243);
            }
            if($image2){
                $this->data['images'][] = $this->model_tool_image->resize($image2, 207, 243);
            }
            if($image3){
                $this->data['images'][] = $this->model_tool_image->resize($image3, 207, 243);
            }
            
            $logo = $manufacturer_info['logo'] ? $manufacturer_info['logo'] : 'no_image.jpg';   
            $this->data['logo'] = $this->model_tool_image->resize($logo, 214, 50);            
            $default_sort = 'pms.product_msort_order';
            
            if (isset($this->request->get['page'])) {
                $page = $this->request->get['page'];
            } 
            else {
                $page = 1;
            }                

            if (isset($this->request->get['sort'])) {
                $sort = $this->request->get['sort'];
            } 
            else {
                $sort = 'p.date_added';
                //$sort = $default_sort;
            }

            if (isset($this->request->get['order'])) {
                $order = $this->request->get['order'];
            } 
            else {
                //$order = 'ASC';
				$order = 'DESC';
            }
                    
            //$limit = 9; //24; //12;
            $limit = $this->isMobileEnabled() ? 8 : 9;

            $url = HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id'];
            $this->document->links[] = array(
                'href' => $this->model_tool_seo_url->rewrite($url),
                'rel'  => 'canonical'
            );
            if($category > 0){
                $url .= '&path=' . $category_path;
            }
            
            $this->data['sorts'] = array();                                         
            $this->data['sorts'][] = array(
                'text'  => $this->language->get('text_newest'),
                'value' => 'p.date_added-DESC',
                'href'  => $this->model_tool_seo_url->rewrite($url . '&sort=p.date_added&order=DESC&limit=' . $limit),
            );                  
            $this->data['sorts'][] = array(
                'text'  => $this->language->get('text_name_asc'),
                'value' => 'pd.name-ASC',
                'href'  => $this->model_tool_seo_url->rewrite($url . '&sort=pd.name&order=ASC&limit=' . $limit),
            );  
            $this->data['sorts'][] = array(
                'text'  => $this->language->get('text_name_desc'),
                'value' => 'pd.name-DESC',
                'href'  => $this->model_tool_seo_url->rewrite($url . '&sort=pd.name&order=DESC&limit=' . $limit),
            );
            $this->data['sorts'][] = array(
                'text'  => $this->language->get('text_price_asc'),
                'value' => 'p.price-ASC',
                'href'  => $this->model_tool_seo_url->rewrite($url . '&sort=p.price&order=ASC&limit=' . $limit),
            );  
            $this->data['sorts'][] = array(
                'text'  => $this->language->get('text_price_desc'),
                'value' => 'p.price-DESC',
                'href'  => $this->model_tool_seo_url->rewrite($url . '&sort=p.price&order=DESC&limit=' . $limit)
            );
			$this->data['sorts'][] = array(
                'text'  => $this->language->get('text_default'),                
                //'value' => 'p.sort_order-ASC',                
                'value' => $default_sort . '-ASC',
                'href'  => $this->model_tool_seo_url->rewrite($url . '&sort=' . $default_sort . '&order=ASC&limit=' . $limit),
            );  
            /*
            $this->data['sorts'][] = array(
                'text'  => $this->language->get('text_category_asc'),
                'value' => 'cd.name-ASC',
                'href'  => $this->model_tool_seo_url->rewrite($url . '&sort=cd.name&order=ASC&limit=' . $limit),
            );    
            */
            
            //$product_total = $this->model_catalog_product->getTotalProductsByManufacturerId($this->request->get['manufacturer_id']);                                 
            //$results = $this->model_catalog_product->getProductsByManufacturerId($this->request->get['manufacturer_id'], $sort, $order, ($page - 1) * $limit, $limit);                            
            if($category > 0){
                $product_total = $this->model_catalog_product->getTotalProductsByConditional($category, $this->request->get['manufacturer_id']);
                $results = $this->model_catalog_product->getProductsByConditional($category, $this->request->get['manufacturer_id'], $sort, $order, ($page - 1) * $limit, $limit);
            }
            else{
                $product_total = $this->model_catalog_manufacturer->getTotalProductsByManufacturer($this->request->get['manufacturer_id']);
                $results = $this->model_catalog_manufacturer->getProductsByManufacturer($this->request->get['manufacturer_id'], $sort, $order, ($page - 1) * $limit, $limit);                
            }
            
            $this->data['products'] = array();
            foreach ($results as $result) {
                if ($result['image']) {
                    $image = $result['image'];
                } 
                else {
                    $image = 'no_image.jpg';
                }
                
                //$shortname = (strlen($result['name']) <= 16) ? $result['name'] : substr($result['name'], 0 ,16) . ' ...';
                $shortname = $this->model_catalog_product->breakProductName($result['name'], 34);
                $path = $this->model_catalog_product->getCategoriesPath($result['product_id']);    
                
                $price = $result['price']; 
                $price_formatted = $this->currency->formatPrice($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
                $discount = 0;
                $discount_formatted = 0; 
                $discount_data = $this->model_catalog_discount->getDiscount($result['product_id']); 
                if($discount_data > 0){
                    $discount = $discount_data;
                    $discount_formatted = $this->currency->formatPrice($this->tax->calculate($discount_data, $result['tax_class_id'], $this->config->get('config_tax'))); 
                }
                    
                $discounts_data = $this->model_catalog_product->getProductDiscounts($result['product_id']);             
                if($discounts_data){   
                    $discount = $discounts_data[0]['price'];                     
                    $discount_formatted = $this->currency->formatPrice($this->tax->calculate($discounts_data[0]['price'], $result['tax_class_id'], $this->config->get('config_tax')));
                }                                         
                   
                //$options = $this->model_catalog_product->getProductOptions($result['product_id']);
                $options = $this->model_catalog_product->getActiveProductOptions($result['product_id']);
                $attribute_price = 0;  
                $min_price = false; 
                $price_ref = $discount ? $discount : $price;               
                foreach($options as $option_item){
                    foreach($option_item['option_value'] as $option_value){ 
                        $option_price = (float)$option_value['price'] ? $this->tax->calculate($option_value['price'], $result['tax_class_id'], $this->config->get('config_tax')) : 0;                    
                        if($option_price && ($option_value['prefix'] == '-')){
                            $option_price = -1 * $option_price;                        
                        }
                        
                        if($option_price < $attribute_price){
                            $attribute_price = $option_price;   
                        }
                        
                        if($option_price > 0){
                            $min_price = true;
                        }
                    }
                }                
                    
                if($attribute_price != 0){
                    $ref_price =  $this->currency->formatPrice($price_ref + $attribute_price);
                }
                elseif($min_price){
                    $ref_price = $this->currency->formatPrice($price_ref);    
                }
                else{
                    $ref_price = 0;
                }

                $rate_discount = ((floatval($price) - floatval($price_ref)) / floatval($price)) * 100;
                $rate_discount = round($rate_discount);

                $this->data['products'][] = array(
                    'name'      => $result['name'],
                    'shortname' => $shortname,
                    'sku'       => $result['sku'],                        
                    //'thumb'     => $this->model_tool_image->resize($image, 121, 148), 
                    'thumb'     => $this->model_tool_image->resize($image, 190, 230), 
                    //'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&manufacturer_id=' . $this->request->get['manufacturer_id'] . '&product_id=' . $result['product_id']),                        
                    'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id'] . '&path=' . $path . '&manufacturer_id=' . $this->request->get['manufacturer_id']),
                    'unbreak'   => mb_strlen($shortname, 'UTF-8') <= 34,
                    'price'     => $price_formatted,
                    'discount'  => $discount_formatted,
                    'ref_price' => $ref_price,
                    'options'   => $options,
                    'rate_discount' => $rate_discount,
                );
            } 
             
            $url = '';           
            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }      
            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }
            if($category > 0){
                $url .= '&path=' . $category_path; //'&category_id=' . $category_path;            
            }
            
            $pagination = new Pagination();
            $pagination->total = $product_total;
            $pagination->page = $page;
            $pagination->limit = $limit;
            $pagination->text = $this->language->get('text_pagination');
            $pagination->url = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id'] . $url . '&page={page}');
            $this->data['pagination'] = $pagination->renderPaging();

            $this->load->model('setting/tooltip'); 
			$this->data['shipping_note'] = $this->model_setting_tooltip->getShippingNote();
                
            $this->data['sort'] = $sort;
            $this->data['order'] = $order;
                
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/manufacturer.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/product/manufacturer.tpl';
            } 
            else {
                $this->template = 'default/template/product/manufacturer.tpl';
            }    
            
            $this->children = array(
                'common/header',
                'common/footer',
                'common/column_left',
                'module/category',
                'module/services',
                'module/newsletter',
            );

            $this->data['lazy_load'] = array(
                'total'     => $pagination->total,
                'page'      => $pagination->page,
                'limit'     => $pagination->limit,
                'url'       => $pagination->url,
            );

            if (isset($this->request->get['lazy_load']) && $this->request->get['lazy_load']) {
                if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/category_lazyload.tpl')) {
                    $this->template = $this->config->get('config_template') . '/template/product/category_lazyload.tpl';
                }
                else {
                    $this->template = $this->config->get('config_template') . '/template/product/search.tpl';
                }
            }

            $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));                                                        
        } else {
            $url = '';             
            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            } 
            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            } 
            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }    
            
            $this->document->breadcrumbs[] = array(
                'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $manufacturer_id . $url),
                'text'      => $this->language->get('text_error'),
                'separator' => $this->language->get('text_separator')
            );
                    
            $this->document->title = $this->language->get('text_error');  
            $this->data['heading_title'] = $this->language->get('text_error'); 
            $this->data['text_error'] = $this->language->get('text_error'); 
            $this->data['button_continue'] = $this->language->get('button_continue'); 
            $this->data['continue'] = false; //HTTP_SERVER . 'index.php?route=common/home';
                  
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
            } 
            else {
                $this->template = 'default/template/error/not_found.tpl';
            }
            
            $this->children = array(
                'common/column_right',
                'common/column_left',
                'common/footer',
                'common/header',
            );        
            
            $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
        }
    }
      
	public function indexOld() { 
		$this->language->load('product/manufacturer');

		$this->load->model('catalog/manufacturer');
		$this->load->model('catalog/product');
		$this->load->model('tool/seo_url'); 
		$this->load->model('tool/image');
		
		$this->document->breadcrumbs = array();
		
      	$this->document->breadcrumbs[] = array(
        	'href'      => HTTP_SERVER . 'index.php?route=common/home',
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	);

		if (isset($this->request->get['manufacturer_id'])) {
			$manufacturer_id = $this->request->get['manufacturer_id'];
		} else {
			$manufacturer_id = 0;
		}
		
		$manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($manufacturer_id);
	
		if ($manufacturer_info) {
      		$this->document->breadcrumbs[] = array(
        		'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id']),
        		'text'      => $manufacturer_info['name'],
        		'separator' => $this->language->get('text_separator')
      		);
					  		
			$this->document->title = $manufacturer_info['name'];
									
			$this->data['heading_title'] = $manufacturer_info['name'];

			$this->data['text_sort'] = $this->language->get('text_sort');
		
			$product_total = $this->model_catalog_product->getTotalProductsByManufacturerId($this->request->get['manufacturer_id']);			
			if ($product_total) {
				if (isset($this->request->get['page'])) {
					$page = $this->request->get['page'];
				} else {
					$page = 1;
				}				

				if (isset($this->request->get['sort'])) {
					$sort = $this->request->get['sort'];
				} else {
					$sort = 'p.sort_order';
				}

				if (isset($this->request->get['order'])) {
					$order = $this->request->get['order'];
				} else {
					$order = 'ASC';
				}
			
				$this->load->model('catalog/review');
				
				$this->data['button_add_to_cart'] = $this->language->get('button_add_to_cart');
				
				$this->data['products'] = array();
        		
				$results = $this->model_catalog_product->getProductsByManufacturerId($this->request->get['manufacturer_id'], $sort, $order, ($page - 1) * $this->config->get('config_catalog_limit'), $this->config->get('config_catalog_limit'));
				
        		foreach ($results as $result) {
					if ($result['image']) {
						$image = $result['image'];
					} else {
						$image = 'no_image.jpg';
					}   					
                
					if ($this->config->get('config_review')) {
						$rating = $this->model_catalog_review->getAverageRating($result['product_id']);	
					} else {
						$rating = false;
					}
					
					$special = FALSE;
					
					$discount = $this->model_catalog_product->getProductDiscount($result['product_id']);
 					
					if ($discount) {
						$price = $this->currency->format($this->tax->calculate($discount, $result['tax_class_id'], $this->config->get('config_tax')));
					} else {
						$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
					
						$special = $this->model_catalog_product->getProductSpecial($result['product_id']);
					
						if ($special) {
							$special = $this->currency->format($this->tax->calculate($special, $result['tax_class_id'], $this->config->get('config_tax')));
						}						
					}
					
					$options = $this->model_catalog_product->getProductOptions($result['product_id']);
					
					if ($options) {
						$add = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id']);
					} else {
						$add = HTTPS_SERVER . 'index.php?route=checkout/cart&product_id=' . $result['product_id'];
					}
					                    
          			$this->data['products'][] = array(
            			'name'    => $result['name'],
						'model'   => $result['model'],
						'rating'  => $rating,
						'stars'   => sprintf($this->language->get('text_stars'), $rating),            			
						'thumb'   => $this->model_tool_image->resize($image, $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
            			'price'   => $price,
            			'options' => $options,
						'special' => $special,
						'href'    => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&manufacturer_id=' . $this->request->get['manufacturer_id'] . '&product_id=' . $result['product_id']),
						'add'	  => $add
          			);                    
        		}
				
				if (!$this->config->get('config_customer_price')) {
					$this->data['display_price'] = TRUE;
				} elseif ($this->customer->isLogged()) {
					$this->data['display_price'] = TRUE;
				} else {
					$this->data['display_price'] = FALSE;
				}

				$url = '';   		
				if (isset($this->request->get['page'])) {
					$url .= '&page=' . $this->request->get['page'];
				}	
				
				$this->data['sorts'] = array();    				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_default'),
					'value' => 'p.sort_order-ASC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=p.sort_order&order=ASC')
				); 				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_name_asc'),
					'value' => 'pd.name-ASC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=pd.name&order=ASC')
				);   
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_name_desc'),
					'value' => 'pd.name-DESC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=pd.name&order=DESC')
				);  
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_price_asc'),
					'value' => 'p.price-ASC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=p.price&order=ASC')
				); 
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_price_desc'),
					'value' => 'p.price-DESC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=p.price&order=DESC')
				);  				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_rating_desc'),
					'value' => 'rating-DESC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=rating&order=DESC')
				);   				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_rating_asc'),
					'value' => 'rating-ASC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=rating&order=ASC')
				);   				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_model_asc'),
					'value' => 'p.model-ASC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=p.model&order=ASC')
				);  
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_model_desc'),
					'value' => 'p.model-DESC',
					'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=p.model&order=DESC')
				);
                
				$url = '';  		
				if (isset($this->request->get['sort'])) {
					$url .= '&sort=' . $this->request->get['sort'];
				}	  
				if (isset($this->request->get['order'])) {
					$url .= '&order=' . $this->request->get['order'];
				}
				
				$pagination = new Pagination();
				$pagination->total = $product_total;
				$pagination->page = $page;
				$pagination->limit = $this->config->get('config_catalog_limit');
				$pagination->text = $this->language->get('text_pagination');
				$pagination->url = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $this->request->get['manufacturer_id'] . $url . '&page={page}');
			
				$this->data['pagination'] = $pagination->render();

				$this->data['sort'] = $sort;
				$this->data['order'] = $order;
				
				if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/manufacturer.tpl')) {
					$this->template = $this->config->get('config_template') . '/template/product/manufacturer.tpl';
				} else {
					$this->template = 'default/template/product/manufacturer.tpl';
				}	
				
				$this->children = array(
					'common/header',
                    'common/footer',
					'common/column_left',
				);		
				
				$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));										
      		} else {
        		$this->document->title = $manufacturer_info['name'];

        		$this->data['heading_title'] = $manufacturer_info['name'];

        		$this->data['text_error'] = $this->language->get('text_empty');

        		$this->data['button_continue'] = $this->language->get('button_continue');

        		$this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';
		
				if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
					$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
				} else {
					$this->template = 'default/template/error/not_found.tpl';
				}
				
				$this->children = array(
					'common/header',
                    'common/footer',
                    'common/column_left',
				);		
				
				$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));					
      		}
    	} else {
			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
				
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}	
			
      		$this->document->breadcrumbs[] = array(
        		'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $manufacturer_id . $url),
        		'text'      => $this->language->get('text_error'),
        		'separator' => $this->language->get('text_separator')
      		);
					
			$this->document->title = $this->language->get('text_error');

      		$this->data['heading_title'] = $this->language->get('text_error');

      		$this->data['text_error'] = $this->language->get('text_error');

      		$this->data['button_continue'] = $this->language->get('button_continue');

      		$this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';
	  			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}
			
			$this->children = array(
				'common/column_right',
				'common/column_left',
				'common/footer',
				'common/header'
			);		
			
			$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
		}
  	}
}
?>
